%% Copyright 2008 Kevin A. Smith 
%% Licensed under the Apache License, Version 2.0 (the "License"); 
%% you may not use this file except in compliance with the License. 
%% You may obtain a copy of the License at 
%%
%% http://www.apache.org/licenses/LICENSE-2.0 
%%
%% Unless required by applicable law or agreed to in writing, software 
%% distributed under the License is distributed on an "AS IS" BASIS, 
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
%% See the License for the specific language governing permissions and 
%% limitations under the License.

-module(error_response_parser).

-compile(export_all).

callback({startElement, _, "Error", _, _}, {_Current, _Flag, All}) ->
    {[], [], All};
callback({startElement, _, "Code", _, _}, {Current, _Flag, All}) ->
    {Current, code, All};
callback({startElement, _, "Message", _, _}, {Current, _Flag, All}) ->
	{Current, message, All};
callback({endElement, _, "Error", _}, {Current, _Flag, All}) ->
	{[], [], lists:append(All, [Current])};
callback({endElement, _, "Errors", _}, {_Current, _Flag, All}) ->
	{error, lists:filter(fun drop_empty/1, lists:reverse(All))};
callback({characters, Text}, {Current, Flag, All}) when is_atom(Flag) ->
	Current1 = lists:append(Current, [{Flag, Text}]),
	{Current1, Flag, All};		
callback(_Event, State) ->
	State.
	
drop_empty(I) ->
	length(I) > 0.